package output;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Scanner;

import person.*;
import organization.*;

public class StudentConsole extends Console {
	private Student student= new Student ();
	private SimpleDateFormat format3 = new SimpleDateFormat("dd.MM.yyyy");
	
	
	public StudentConsole (){
		consoleStr.add("Get Group List;");
		consoleStr.add("Get Student Info by name;");
		consoleStr.add("Add new Student;");
		consoleStr.add("Add Student to group;");
		consoleStr.add("Get Student list in group;");
		consoleStr.add("Return to main menu.");
	}

	
	public void inputInConsole() {	
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter option:");
		while (true) {
			while (scan.hasNextLine()) {
				if (scan.hasNextInt()) {
					int opt = scan.nextInt();
					if (1 <= opt & opt <= consoleStr.size()) {
					if (opt == 1) {
						getGroupList();
						getConsole();
						return;
					}
					if (opt == 2) {
						getStudByName();
						getConsole();
						return;
					}
					if (opt == 3) {
						addStudent(AddPersonal());
						getConsole();
						return;
					}
					if (opt == 4) {
						addToGroup();
						getConsole();
						return;
					}
					if (opt == 5) {
						getSudentsBygroup();
						getConsole();
						return;
					}
					if (opt == 6) {
						return;
					}
					}else{
						System.out.println("Icorrect point!");
					}
					System.out.println("Student Menu \r\n__________________________\r\nPlease enter option:");
				}else {
					System.out.println("Should be numeric! Please enter option:");
					scan.next();
				}
			}
		}
	}
	public ArrayList <Group> getGroupList (){
		ArrayList <Student> stdList = Base.getSortlist(student);
		ArrayList<Group> grp = new ArrayList <Group>();
		if (Group.getMaxGroupID() == 1){
			System.out.println("Groups not found!");
			return null;
		}
		
		for (int i=1; i<Group.getMaxGroupID();i++){
			for (int j=0;j<stdList.size();j++){
				if (stdList.get(j).getGroup().getGrpID() == i){
					System.out.println(i+". "+stdList.get(j).getGroup().getGrpName()+";");
					grp.add(stdList.get(j).getGroup());
					break;
				}
			}
		
		}
		return grp;
	}
	private void getSudentsBygroup (){
		int count = 1;
		ArrayList <Student> stdList = Base.getSortlist(student);
		Scanner scan = new Scanner (System.in);
		ArrayList <Group> Grp = getGroupList();
		if (Grp != null){
			
		System.out.println("Please choose Group:");
			while(scan.hasNextLine()){
				if (scan.hasNextInt()){
					int opt = scan.nextInt();
					if (opt>0 & opt<=Grp.size()){
						for (int i=0;i<stdList.size();i++){
							if (stdList.get(i).getGroup().equals(Grp.get(opt-1))){
								System.out.println(count+". "+stdList.get(i).getFirstAndScndName()+";");
								count++;
							}
						}
					}else{
						System.out.println("Incorrect point!");
					}
				}else{
					System.out.println("Should be numeric");
				}
			}
		}
	}
	private void getStudByName (){
		ArrayList <Student> stdList = Base.getSortlist(student);
		System.out.println ("Pelase enter first name:");
		Scanner scan = new Scanner (System.in);
		String firstName = scan.next();
		System.out.println("Please enter second name:");
		String secndName =  scan.next();
		
		
		Student parsedStudent = Base.getPersonByName(firstName, secndName, student);
		
		if (parsedStudent != null) {
			System.out.println("Name:" + parsedStudent.getFirstAndScndName()
					+ ";");
			System.out.println("Born in:" + parsedStudent.getPlaceOfBirth()
					+ " " + format3.format(parsedStudent.getBirth().getTime())
					+ ";");
			if (parsedStudent.getGroup() != null) {
				System.out.println("Group name:"
						+ parsedStudent.getGroup().getGrpName() + ". Group ID "
						+ parsedStudent.getGroup().getGrpID() + ";");
			} else {
				System.out.println("Group not set.");
			}
		}
		
		
	}
	private void addToGroup (){
		ArrayList <Student> stdList = Base.getSortlist(student);
		System.out.println("Pelase enter student.");
		Scanner scan = new Scanner (System.in);
		System.out.println("Pelase enter First name:");
		String firstName = scan.next();
		System.out.println("Please enter Second name");
		String secondName = scan.next();
		
		ArrayList <Student> foundStud = new ArrayList <Student>();
		Student requiredObj;
		
		for (int i=0;i<stdList.size();i++){
			if (stdList.get(i).getFirstName().equals(firstName)){
				if (stdList.get(i).getSecondName().equals(secondName)){
					foundStud.add(stdList.get(i));
				}
			}
		}
		if (foundStud.size()<1){
			System.out.println("Student not found: \r\n Please enter 1 to try one more time. \r\n Please enter any to exit.");
			String opt = scan.next();
			if (opt.equals("1")){
				addToGroup();
				return;
			}else{
				return;
			}
		}
		if (foundStud.size()==1){
			requiredObj = foundStud.get(0);
		}else {
			System.out.println("Pelase choose required sudent:");
			for (int i = 0;i<foundStud.size();i++){
				System.out.println((i+1)+". "+foundStud.get(i).getFirstName()+" .Born: "+foundStud.get(i).getBirth().toString()+" "+foundStud.get(i).getPlaceOfBirth());
			}
			int opt = scan.nextInt();
			if (opt > 0 & opt <= (foundStud.size()+1)){
				requiredObj = foundStud.get((opt-1));
				opt = 0;
			}else{
				System.out.println("Entered incorrect option");
				return;
			}
		}
			
			System.out.println("Add to new group?: \r\n 1. Yes; \r\n 2. No;");
			int opt = scan.nextInt();
			if (opt == 1){
				System.out.println("Enter group name:");
				String grpName = scan.next();
				System.out.println("Please enter course:");
				int course =  scan.nextInt();
				requiredObj.addToGroup(new Group (grpName,course));
				System.out.println("Student "+requiredObj.getFirstAndScndName()+" adde to "+requiredObj.getGroup().getGrpName()+" group");
			}
			if (opt == 2){
				ArrayList <Group> grpList = new ArrayList <Group>();
				for (int i=1;i <= Group.getMaxGroupID();i++){
					for (int j = 0;j<stdList.size();j++){
					if (stdList.get(j).getGroup() != null) {
						if (stdList.get(j).getGroup().getGrpID() == i) {
							grpList.add(stdList.get(j).getGroup());
							break;
						}
					}
					}
				}
				System.out.println("Pelase choose group:");
				for (int i=0;i<grpList.size();i++){
					System.out.print((i+1)+". "+grpList.get(i).getGrpName()+" Course "+grpList.get(i).getCourse()+"; ");
					if ((i % 4)==0){
						System.out.println();
					}
				}
				opt = scan.nextInt();
				if (opt >0 & opt<=(grpList.size()+1)){
					requiredObj.addToGroup(grpList.get((opt-1)));
					System.out.println("Student "+requiredObj.getFirstAndScndName()+" adde to "+requiredObj.getGroup().getGrpName()+" group");
				}
			}
	}
	
	private void addStudent (PersonInfo info){
		if (info != null) {
			
			Base.addPerson(new Student(info));
			
		}
	}
	
}
